/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 62536
 * Date: 2024-06-14
 * Time: 16:44
 */
public class Solution {
    public static void main(String[] args) {
        int[][] arr = {{-2,-3,3}, {-5,-10,1}, {10,35,-5}};
        System.out.println(calculateMinimumHP(arr));
    }
    public static int calculateMinimumHP(int[][] dungeon) {
        int m = dungeon.length;
        int n = dungeon[0].length;
        int[][] dp = new int[m+1][n+1];
        for (int i = 0; i <= m; i++) {
            dp[i][n] = Integer.MAX_VALUE;
        }
        for (int i = 0; i <= n; i++) {
            dp[m][i] = Integer.MAX_VALUE;
        }


        for (int i = m-1; i >= 0; i--) {
            for (int j = n-1; j >= 0; j--) {
                if(i == m-1 && j == n-1){
                    dp[i][j] = 1 - dungeon[i][j];
                } else {
                    dp[i][j] = Math.min(dp[i][j+1], dp[i+1][j]) - dungeon[i][j];
                }

                dp[i][j] = Math.max(1, dp[i][j]);
            }
        }

        return dp[0][0];
    }
}
